﻿using AutoMapper;
using CoreProject.DTO;
using CoreProject.IService;
using CoreProject.Model;
using Microsoft.AspNetCore.Mvc;
using MVCManage.Api;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace CoreProject.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class DeptController : ControllerBase
    {
        private readonly IMapper _mapper;
        private readonly IDeptService _service;

        public DeptController(IMapper mapper,IDeptService service)
        {
            _mapper = mapper;
            _service = service;
        }

        [HttpGet]
        public async Task<ResultData<List<Department>>> Get()
        {
            var data = await _service.GetListAsync(true, r =>r.DeptId, r => true);
            var list = _mapper.Map<List<Department>>(data);
            ResultData<List<Department>> result = new ResultData<List<Department>>();
            result.code = "1";
            result.msg = "ok";
            result.state = 200;
            result.data = list;
            return result;
        }

        // GET api/<DepartmentsController>/5
        [HttpGet("page")]
        public async Task<ResultPageList<DepartmentsDto>> GetListByPage(int page, int size, string name = "")
        {
            var data = await _service.GetDeptBypage(name, page, size);
            var list = _mapper.Map<List<DepartmentsDto>>(data.list);
            ResultPageList<DepartmentsDto> result = new ResultPageList<DepartmentsDto>();
            result.state = 200;
            result.code = "1";
            result.msg = "ok";
            result.page = page;
            result.size = size;
            result.count = data.count;
            result.data =list;
            return result;
        }

        // POST api/<DepartmentsController>
        [HttpPost]
        public async Task<ResultHandle> Post([FromBody] DepartmentsDto Department)
        {
            var data = _mapper.Map<Department>(Department);
            data.LastTime = DateTime.Now;
            var bol = await _service.AddAsync(data);
            ResultHandle result = new ResultHandle();
            result.state = 200;
            result.code = bol ? "1" : "-1";
            result.msg = bol ? "添加成功!" : "添加失败!";
            return result;

        }

        // PUT api/<DepartmentsController>/5
        [HttpPut]
        public async Task<ResultHandle> Put([FromBody] DepartmentsDto Department)
        {

            var data = _mapper.Map<Department>(Department);
            var bol = await _service.UpdateAsync(data);
            ResultHandle result = new ResultHandle();
            result.state = 200;
            result.code = bol ? "1" : "-1";
            result.msg = bol ? "修改成功!" : "修改失败!";
            return result;

        }

        // DELETE api/<DepartmentsController>/5
        [HttpDelete]
        public async Task<ResultHandle> Delete(int id)
        {
            Department dept = new Department();
            dept.DeptId = id;
            var bol = await _service.DeleteAsync(dept);
            ResultHandle result = new ResultHandle();
            result.state = 200;
            result.code = bol ? "1" : "-1";
            result.msg = bol ? "删除成功!" : "删除失败!";
            return result;
        }
        [HttpDelete("DeleteBatch")]
        public async Task<ResultHandle> DeleteBatch([FromBody] int[] ids)
        {
            Department[] dept = new Department[ids.Length];
            for (int i = 0; i < ids.Length; i++)
            {
                dept[i] = new Department() { DeptId = ids[i] };
            }

            var bol = await _service.DeleteRangeAsync(dept);
            ResultHandle result = new ResultHandle();
            result.state = 200;
            result.code = bol ? "1" : "-1";
            result.msg = bol ? "删除成功!" : "删除失败!";
            return result;
        }

    }
}
